---@class BattleCard : UIComponentBase
---@field fguiObj GComponent
local BattleCard = Class("BattleCard",UIComponentBase)
function BattleCard:OnCreate()
    self.modelName = ""
    self.text_name = self:GetChild("Text_Name")
    self.textCost = self:GetChild("Text_Cost")
    self.ctrlNext = self:GetController("isNext")
    self.cardImage = self:GetChild("Loader_CardIcon")
    ---@type Battle.Card
    self.card = nil
    self.isCostEnough = false
    self:RegisterOnClick(self.fguiObj,self.ClickCard)
    self:RegisterTouchBegin(self.fguiObj,self.TouchBegin)
    self:RegisterTouchMove(self.fguiObj,self.TouchMove)
    self:RegisterTouchEnd(self.fguiObj,self.TouchEnd)
    self.group = {isCreate = false,goList = {},center = Vector3.New()}
    self.originPos = Vector2.New(self.fguiObj.x,self.fguiObj.y)
    self.positionEffectGo = nil
    self.moveThreshold = 100
end
function BattleCard:OnDestroy()
    self.card = nil
    if self.group then
        for k,v in pairs(self.group.goList) do
            resMgr:ReleaseInstantiate(v)
        end
    end
    self.group = nil
end
---@param card Battle.Card
function BattleCard:SetCard(card,index)
    self.card = card
    self.index = index
    self.isNext = self.index == 5
    self.modelName = DataTable.GetById("t_heroresources",self.card.heroId,"f_HeroModel")
    self.modelName = "Prefab/Hero/"..self.modelName
    self.fguiObj:SetXY(self.originPos.x,self.originPos.y)
    self.fguiObj.alpha = 1
    self:Refresh()
end

function BattleCard:Refresh()
    self.text_name.text = DataTable.GetById("t_hero",self.card.heroId,"f_HeroName")
    self.textCost.text = tostring(self.card.cost)
    self.ctrlNext.selectedIndex = self.isNext and 1 or 0
    self.cardImage.url = IconUtil.GetHeroCardIconUrl(self.card.heroId)
end
function BattleCard:ClickCard()

end
function BattleCard:TouchBegin()
    if self.isNext then return end
    self.isCostEnough = Battle.PlayerSystem.GetMyPlayer().energy >= self.card.cost
    if self.isCostEnough then
        Battle.GlobalEventSystem.BroadCast(Battle.Event.ShowPutAreaEffect,Battle.EventArgs.ShowPutAreaEffectArgs:Create(true))
    end
end
---@param evt FairyGUI.EventContext
function BattleCard:TouchMove(evt)
    if self.isNext then return end
    if not self.isCostEnough then return end
    local x = evt.inputEvent.x
    local y = evt.inputEvent.y
    if y > self.originPos.y then
        y = self.originPos.y
    end
    local alpha =  1- math.min(self.originPos.y - y,self.moveThreshold)/self.moveThreshold
    self.fguiObj:SetXY(x,y)
    self.fguiObj.alpha = alpha
    if self.card.num == 0 then
        logError("英雄数量不能为0")
        return
    end
    if not self.group.isCreate then
        if self.originPos.y - y > self.moveThreshold then
            self:CreateHandModel(self.modelName)
        end
    end
    self:SetInstGoPos()
end
function BattleCard:SetGray(isGray)
    self.fguiObj.grayed = isGray
end
function BattleCard:LoadPositionEffectFinished(go)
    self.positionEffectGo = go
    Battle.SceneMgr.AddChild(go.transform)
end
function BattleCard:CreateHandModel(name)
    local go
    for i = 1,self.card.num do
        local go = Battle.PoolMgr.GetPrefabFromPool(name)
        if not go then
            go = resMgr:Instantiate(name)
        end
        Util.SetLocalRotation(go,0,0,0)
        local scale = DataTable.GetById("t_hero",self.card.heroId,"f_BattleSizeZoom")/100
        Util.SetLocalScale(go,scale,scale,scale)

        table.insert(self.group.goList,go)
        Battle.SceneMgr.AddChild(go.transform)
        --

    end
    Battle.PoolMgr.LoadPrefabFromPool(Battle.EFFECT_PATH.."luoka_position",self.LoadPositionEffectFinished,self)
    --初始化方阵位置
    local num = self.card.num
    local row = math.floor(math.sqrt(num))
    local col = row
    local extraNum = num - row*col
    if extraNum > 0 then
        if extraNum > col then
            row = row + 1
        end
        col = col + 1
        extraNum = col - row*col + num
    end
    local hr = math.floor(row/2)
    local hc = math.floor(col/2)
    local step = 1
    local offsetRow = row % 2 == 0 and step/2 or 0
    local offsetCol = col % 2 == 0 and step/2 or 0
    for k,v in ipairs(self.group.goList) do
        local r = math.floor((k-1) / col)
        local c = (k-1) % col
        local x = (c - hc) * step + offsetCol
        local y = (r - hr) * step + offsetRow
        if extraNum > 0 and r == row - 1 then
            local offsetCol = (extraNum) % 2 == 0 and step/2 or 0
            local hc  = math.floor(extraNum/2)
            x = (c - hc) * step + offsetCol
        end
        Util.SetPosition(v,y,0,x)
    end
    self.group.center:Set(0,0,0)
    self.group.isCreate = true
end
function BattleCard:SetInstGoPos()
    if not self.group.isCreate then return end
    local flag,wx,wy,wz = Battle.SceneMgr.GetRaycastScenePos()
    if flag then
        wx,wy,wz = Battle.SceneMgr.LimitPutArea(wx,wy,wz)
        local height = DataTable.GetById("t_hero",self.card.heroId,"f_BattleFlightAltitude")
        for k,v in ipairs(self.group.goList) do
            local offX =  wx - self.group.center.x
            local offZ = wz - self.group.center.z
            local x,y,z = Util.GetPosition(v,nil,nil,nil)
            y = height/100
            Util.SetPosition(v,x+offX,y,z+offZ)
        end
        self.group.center:Set(wx,0,wz)
        if self.positionEffectGo then
            Util.SetPosition(self.positionEffectGo,wx,0,wz)
        end
        return flag,wx,wy,wz
    end
end
---@param evt FairyGUI.EventContext
function BattleCard:TouchEnd(evt)
    if self.isNext then return end
    if not self.isCostEnough then return end
    self.fguiObj:SetXY(self.originPos.x,self.originPos.y)
    self.fguiObj.alpha = 1
    Battle.GlobalEventSystem.BroadCast(Battle.Event.ShowPutAreaEffect,Battle.EventArgs.ShowPutAreaEffectArgs:Create(false))
    if not self.group.isCreate then return end
    local x = evt.inputEvent.x
    local y = evt.inputEvent.y
    local pos = self.fguiObj:GlobalToLocal(Vector2(x,y))
    Battle.PoolMgr.ReturnPrefabToPool(Battle.EFFECT_PATH.."luoka_position",self.positionEffectGo)
    self.positionEffectGo = nil
    if  pos.y < self.fguiObj.height/2 and pos.y > -self.fguiObj.height/2 then
        for k,v in pairs(self.group.goList) do
            Battle.PoolMgr.ReturnPrefabToPool(self.modelName,v)
        end
        self:GroupReset()
        return
    end
    local flag = self:SetInstGoPos()

    if flag then
        local player = Battle.PlayerSystem.GetMyPlayer()
        for k,v in pairs(self.group.goList) do
            local e = v.transform:Find("position_effect")
            local x,y,z = Util.GetPosition(v,nil,nil,nil)
            Battle.PoolMgr.ReturnPrefabToPool(self.modelName,v)
            player:CreateCardEntity(self.card.heroId,x,z)
        end
        player:UseHandCard(self.index)
    end
    self:GroupReset()
end
function BattleCard:GroupReset()
    self.group.goList = {}
    self.group.isCreate = false
end
return BattleCard